import { Form, Modal, message } from 'antd'
import React, { useEffect } from 'react'
import { $get } from '@js/authRequest'
import { awaitWrap } from '@src/assets/js/tool'
import Text from '@components/Text'

const { Item, useForm } = Form

async function getDetailRequest (id) {
  const res = await $get(`/api/xapc/arbitrator/${id}`)
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

async function initData (id, formRef) {
  const h = message.loading('加载中', 120)
  const [e, d] = await awaitWrap(getDetailRequest(id))
  h()
  if (e === null && d !== null) {
    formRef.setFieldsValue({ credit_record: d.credit_record })
  }
}

function Main ({ visible, hide, arbitratorid }) {
  const [formRef] = useForm()
  useEffect(() => {
    if (visible && arbitratorid && formRef) {
      initData(arbitratorid, formRef)
    } else if (!visible && formRef) {
      formRef.resetFields()
    }
  }, [visible, arbitratorid, formRef])
  return (
    <Modal title='诚信档案详情' open={visible} onCancel={hide} forceRender maskClosable={false} footer={null}>
      <Form form={formRef}>
        <Item label='诚信档案' name='credit_record'>
          <Text />
        </Item>
      </Form>
    </Modal>
  )
}

export default Main
